//! UTF-8 解码器状态机相关常量定义

/// 表示成功接受一个UTF-8字符
pub const UTF8_ACCEPT: u8 = 0;

/// 表示遇到无效的UTF-8字符
pub const UTF8_REJECT: u8 = 1;

/// DFA状态转换表
/// 该表包含400个条目，用于UTF-8解码的有限状态机转换规则
pub static UTF8D: [u8; 400] = [
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
    8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    0x0a,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x04,0x03,0x03,
    0x0b,0x06,0x06,0x06,0x05,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,
    0x00,0x01,0x02,0x03,0x05,0x08,0x07,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x01,0x01,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1,
    1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,
    1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1,
    1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
];


/// 解码单个UTF-8字节并更新解码状态
/// 
/// # 参数
/// - state: 当前解码状态（可变引用）
/// - codepoint: 当前解码的码点（可变引用）
/// - byte: 输入的字节数据
/// 
/// # 返回值
/// 更新后的解码状态，可能为UTF8_ACCEPT或UTF8_REJECT
pub fn decode_utf8(state: &mut u32, codepoint: &mut u32, byte: u8) -> u32 {
    let type_ = UTF8D[byte as usize];
    
    *codepoint = if *state != UTF8_ACCEPT as u32 {
        (byte & 0x3f) as u32 | (*codepoint << 6)
    } else {
        (0xffu32 >> type_) & byte as u32
    };
    
    let index = 256 + (*state as usize) * 16 + type_ as usize;
    *state = UTF8D.get(index).copied().unwrap_or(UTF8_REJECT) as u32;
    
    *state
}